<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.json',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="函数的调用信息是程序中比较重要运行期信息, 在很多场合都会用到(比如调试或日志)。 Go 语言 runtime 包的 runtime.Caller / runtime.Callers / runtime.FuncForPC 等几个函数提供了获取函数调用者信息的方法. 这几个函数的文档链接:  http://golang.org/pkg/runtime/#Caller  http://golang.">
<meta name="keywords" content="golang">
<meta property="og:type" content="article">
<meta property="og:title" content="golang_runtime函数调用信息">
<meta property="og:url" content="https://unix2dos.github.io/p/375281af.html">
<meta property="og:site_name" content="Levon&#39;s Blog">
<meta property="og:description" content="函数的调用信息是程序中比较重要运行期信息, 在很多场合都会用到(比如调试或日志)。 Go 语言 runtime 包的 runtime.Caller / runtime.Callers / runtime.FuncForPC 等几个函数提供了获取函数调用者信息的方法. 这几个函数的文档链接:  http://golang.org/pkg/runtime/#Caller  http://golang.">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-09-27T15:36:51.096Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="golang_runtime函数调用信息">
<meta name="twitter:description" content="函数的调用信息是程序中比较重要运行期信息, 在很多场合都会用到(比如调试或日志)。 Go 语言 runtime 包的 runtime.Caller / runtime.Callers / runtime.FuncForPC 等几个函数提供了获取函数调用者信息的方法. 这几个函数的文档链接:  http://golang.org/pkg/runtime/#Caller  http://golang.">
  <link rel="canonical" href="https://unix2dos.github.io/p/375281af">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>golang_runtime函数调用信息 | Levon's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Levon's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">微信:  L6241425</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>时光</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://unix2dos.github.io/p/375281af.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Levon">
      <meta itemprop="description" content="Never Give Up">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Levon's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">golang_runtime函数调用信息

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2018-07-07 14:19:52" itemprop="dateCreated datePublished" datetime="2018-07-07T14:19:52+08:00">2018-07-07</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-09-27 23:36:51" itemprop="dateModified" datetime="2019-09-27T23:36:51+08:00">2019-09-27</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/1-编程语言/" itemprop="url" rel="index"><span itemprop="name">1-编程语言</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/1-编程语言/golang/" itemprop="url" rel="index"><span itemprop="name">golang</span></a></span>

                
                
              
            </span>
          

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
        
      
      <span class="post-meta-item-text">Disqus：</span>
    
    <a title="disqus" href="/p/375281af.html#comments" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="p/375281af.html" itemprop="commentCount"></span></a>
  </span>
  
  
          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              
                <span class="post-meta-item-text">本文字数：</span>
              
              <span>12k</span>
            </span>
          
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              
              <span>11 分钟</span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>函数的调用信息是程序中比较重要运行期信息, 在很多场合都会用到(比如调试或日志)。</p>
<p>Go 语言 <code>runtime</code> 包的 <code>runtime.Caller</code> / <code>runtime.Callers</code> / <code>runtime.FuncForPC</code> 等几个函数提供了获取函数调用者信息的方法.</p>
<p>这几个函数的文档链接:</p>
<ul>
<li><p><a href="http://golang.org/pkg/runtime/#Caller" target="_blank" rel="noopener">http://golang.org/pkg/runtime/#Caller</a></p>
</li>
<li><p><a href="http://golang.org/pkg/runtime/#Callers" target="_blank" rel="noopener">http://golang.org/pkg/runtime/#Callers</a></p>
</li>
<li><p><a href="http://golang.org/pkg/runtime/#FuncForPC" target="_blank" rel="noopener">http://golang.org/pkg/runtime/#FuncForPC</a></p>
</li>
</ul>
<h2 id="runtime-Caller的用法-常用"><a href="#runtime-Caller的用法-常用" class="headerlink" title="runtime.Caller的用法(常用)"></a>runtime.Caller的用法(常用)</h2><p>函数的签名如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">func runtime.Caller(skip int) (pc uintptr, file string, line int, ok bool)</span><br></pre></td></tr></table></figure>

<p><code>runtime.Caller</code> 返回当前 <code>goroutine</code> 的栈上的函数调用信息. 主要有当前的<code>pc</code> 值和调用的文件和行号等信息. 若无法获得信息, 返回的 <code>ok</code> 值为 <code>false</code>.</p>
 <a id="more"></a>

<p>其输入参数 <code>skip</code> 为要跳过的栈帧数, 若为 <code>0</code> 则表示 <code>runtime.Caller</code> 的调用者.</p>
<p><em>注意:由于历史原因, runtime.Caller 和 runtime.Callers 中的 skip 含义并不相同, 后面会讲到.</em></p>
<p>下面是一个简单的例子, 打印函数调用的栈帧信息:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">func main() &#123;</span><br><span class="line">	for skip := 0; skip &lt; 3; skip++ &#123;</span><br><span class="line">		pc, file, line, ok := runtime.Caller(skip)</span><br><span class="line">		if !ok &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v, pc = %v, file = %v, line = %v\n&quot;, skip, pc, file, line)</span><br><span class="line">	&#125;</span><br><span class="line">	// Output:</span><br><span class="line">	// skip = 0, pc = 4198453, file = caller.go, line = 10</span><br><span class="line">	// skip = 1, pc = 4280066, file = $(GOROOT)/src/pkg/runtime/proc.c, line = 220</span><br><span class="line">	// skip = 2, pc = 4289712, file = $(GOROOT)/src/pkg/runtime/proc.c, line = 1394</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="runtime-Callers的用法"><a href="#runtime-Callers的用法" class="headerlink" title="runtime.Callers的用法"></a>runtime.Callers的用法</h2><p>函数的签名如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">func runtime.Callers(skip int, pc []uintptr) int</span><br></pre></td></tr></table></figure>

<p><code>runtime.Callers</code> 函数和 <code>runtime.Caller</code> 函数虽然名字相似(多一个后缀<code>s</code>), 但是函数的参数/返回值和参数的意义都有很大的差异.</p>
<p><code>runtime.Callers</code> 把调用它的函数Go程栈上的程序计数器填入切片 <code>pc</code> 中. 参数 <code>skip</code> 为开始在 pc 中记录之前所要跳过的栈帧数, <strong>若为 0 则表示 runtime.Callers 自身的栈帧, 若为 1 则表示调用者的栈帧</strong>. 该函数返回写入到 <code>pc</code> 切片中的项数(受切片的容量限制).</p>
<p>下面是 <code>runtime.Callers</code> 的例子, 用于输出每个栈帧的 <code>pc</code> 信息:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">func main() &#123;</span><br><span class="line">	pc := make([]uintptr, 1024)</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		n := runtime.Callers(skip, pc)</span><br><span class="line">		if n &lt;= 0 &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v, pc = %v\n&quot;, skip, pc[:n])</span><br><span class="line">	&#125;</span><br><span class="line">	// Output:</span><br><span class="line">	// skip = 0, pc = [4304486 4198562 4280114 4289760]</span><br><span class="line">	// skip = 1, pc = [4198562 4280114 4289760]</span><br><span class="line">	// skip = 2, pc = [4280114 4289760]</span><br><span class="line">	// skip = 3, pc = [4289760]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出新的 <code>pc</code> 长度和 <code>skip</code> 大小有逆相关性. <code>skip = 0</code> 为 <code>runtime.Callers</code> 自身的信息.</p>
<p>这个例子比前一个例子多输出了一个栈帧, 就是因为多了一个 <code>runtime.Callers</code> 栈帧的信息 (前一个例子是没有 <code>runtime.Caller</code> 信息的(<em>注意:没有 s 后缀</em>)).</p>
<h2 id="runtime-Callers-和-runtime-Caller-的异同"><a href="#runtime-Callers-和-runtime-Caller-的异同" class="headerlink" title="runtime.Callers 和 runtime.Caller 的异同"></a>runtime.Callers 和 runtime.Caller 的异同</h2><p>因为前面2个例子为不同的程序, 输出的 <code>pc</code> 值并不具备参考性. 现在我们看看在同一个例子的输出结果如何:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">func main() &#123;</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		pc, file, line, ok := runtime.Caller(skip)</span><br><span class="line">		if !ok &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v, pc = %v, file = %v, line = %v\n&quot;, skip, pc, file, line)</span><br><span class="line">	&#125;</span><br><span class="line">	// Output:</span><br><span class="line">	// skip = 0, pc = 4198456, file = caller.go, line = 10</span><br><span class="line">	// skip = 1, pc = 4280962, file = $(GOROOT)/src/pkg/runtime/proc.c, line = 220</span><br><span class="line">	// skip = 2, pc = 4290608, file = $(GOROOT)/src/pkg/runtime/proc.c, line = 1394</span><br><span class="line">	pc := make([]uintptr, 1024)</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		n := runtime.Callers(skip, pc)</span><br><span class="line">		if n &lt;= 0 &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v, pc = %v\n&quot;, skip, pc[:n])</span><br><span class="line">	&#125;</span><br><span class="line">	// Output:</span><br><span class="line">	// skip = 0, pc = [4305334 4198635 4280962 4290608]</span><br><span class="line">	// skip = 1, pc = [4198635 4280962 4290608]</span><br><span class="line">	// skip = 2, pc = [4280962 4290608]</span><br><span class="line">	// skip = 3, pc = [4290608]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>比如输出结果可以发现, <code>4280962</code> 和 <code>4290608</code> 两个 <code>pc</code> 值是相同的. 它们分别对应 <code>runtime.main</code> 和 <code>runtime.goexit</code> 函数.</p>
<p><code>runtime.Caller</code> 输出的 <code>4198456</code> 和 <code>runtime.Callers</code> 输出的 <code>4198635</code> 并不相同. 这是因为, 这两个函数的调用位置并不相同, 因此导致了 <code>pc</code> 值也不完全相同.</p>
<p>最后就是 <code>runtime.Callers</code> 多输出一个 <code>4305334</code> 值, 对应<code>runtime.Callers</code>内部的调用位置.</p>
<p>由于Go语言(Go1.2)采用分段堆栈, 因此不同的 <code>pc</code> 之间的大小关系并不明显.</p>
<h2 id="runtime-FuncForPC-的用途"><a href="#runtime-FuncForPC-的用途" class="headerlink" title="runtime.FuncForPC 的用途"></a>runtime.FuncForPC 的用途</h2><p>函数的签名如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">func runtime.FuncForPC(pc uintptr) *runtime.Func</span><br><span class="line">func (f *runtime.Func) FileLine(pc uintptr) (file string, line int)</span><br><span class="line">func (f *runtime.Func) Entry() uintptr</span><br><span class="line">func (f *runtime.Func) Name() string</span><br></pre></td></tr></table></figure>

<p>其中 <code>runtime.FuncForPC</code> 返回包含给定 <code>pc</code> 地址的函数, 如果是无效 <code>pc</code> 则返回 <code>nil</code> .</p>
<p><code>runtime.Func.FileLine</code> 返回与 <code>pc</code> 对应的源码文件名和行号. 安装文档的说明, 如果<code>pc</code>不在函数帧范围内, 则结果是不确定的.</p>
<p><code>runtime.Func.Entry</code> 对应函数的地址. </p>
<p><code>runtime.Func.Name</code> 返回该函数的名称. </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">func main() &#123;</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		pc, _, _, ok := runtime.Caller(skip)</span><br><span class="line">		if !ok &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		p := runtime.FuncForPC(pc)</span><br><span class="line">		file, line := p.FileLine(0)</span><br><span class="line"></span><br><span class="line">		fmt.Printf(&quot;skip = %v, pc = %v\n&quot;, skip, pc)</span><br><span class="line">		fmt.Printf(&quot;  file = %v, line = %d\n&quot;, file, line)</span><br><span class="line">		fmt.Printf(&quot;  entry = %v\n&quot;, p.Entry())</span><br><span class="line">		fmt.Printf(&quot;  name = %v\n&quot;, p.Name())</span><br><span class="line">	&#125;</span><br><span class="line">	// Output:</span><br><span class="line">	// skip = 0, pc = 4198456</span><br><span class="line">	//   file = caller.go, line = 8</span><br><span class="line">	//   entry = 4198400</span><br><span class="line">	//   name = main.main</span><br><span class="line">	// skip = 1, pc = 4282882</span><br><span class="line">	//   file = $(GOROOT)/src/pkg/runtime/proc.c, line = 179</span><br><span class="line">	//   entry = 4282576</span><br><span class="line">	//   name = runtime.main</span><br><span class="line">	// skip = 2, pc = 4292528</span><br><span class="line">	//   file = $(GOROOT)/src/pkg/runtime/proc.c, line = 1394</span><br><span class="line">	//   entry = 4292528</span><br><span class="line">	//   name = runtime.goexit</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	pc := make([]uintptr, 1024)</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		n := runtime.Callers(skip, pc)</span><br><span class="line">		if n &lt;= 0 &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v, pc = %v\n&quot;, skip, pc[:n])</span><br><span class="line">		for j := 0; j &lt; n; j++ &#123;</span><br><span class="line">			p := runtime.FuncForPC(pc[j])</span><br><span class="line">			file, line := p.FileLine(0)</span><br><span class="line"></span><br><span class="line">			fmt.Printf(&quot;  skip = %v, pc = %v\n&quot;, skip, pc[j])</span><br><span class="line">			fmt.Printf(&quot;    file = %v, line = %d\n&quot;, file, line)</span><br><span class="line">			fmt.Printf(&quot;    entry = %v\n&quot;, p.Entry())</span><br><span class="line">			fmt.Printf(&quot;    name = %v\n&quot;, p.Name())</span><br><span class="line">		&#125;</span><br><span class="line">		break</span><br><span class="line">	&#125;</span><br><span class="line">	// Output:</span><br><span class="line">	// skip = 0, pc = [4307254 4198586 4282882 4292528]</span><br><span class="line">	//   skip = 0, pc = 4307254</span><br><span class="line">	//     file = $(GOROOT)/src/pkg/runtime/runtime.c, line = 315</span><br><span class="line">	//     entry = 4307168</span><br><span class="line">	//     name = runtime.Callers</span><br><span class="line">	//   skip = 0, pc = 4198586</span><br><span class="line">	//     file = caller.go, line = 8</span><br><span class="line">	//     entry = 4198400</span><br><span class="line">	//     name = main.main</span><br><span class="line">	//   skip = 0, pc = 4282882</span><br><span class="line">	//     file = $(GOROOT)/src/pkg/runtime/proc.c, line = 179</span><br><span class="line">	//     entry = 4282576</span><br><span class="line">	//     name = runtime.main</span><br><span class="line">	//   skip = 0, pc = 4292528</span><br><span class="line">	//     file = $(GOROOT)/src/pkg/runtime/proc.c, line = 1394</span><br><span class="line">	//     entry = 4292528</span><br><span class="line">	//     name = runtime.goexit</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>根据测试, 如果是无效 <code>pc</code> (比如<code>0</code>), <code>runtime.Func.FileLine</code> 一般会输出当前函数的开始行号. 不过在实践中, 一般会用 <code>runtime.Caller</code> 获取文件名和行号信息, <code>runtime.Func.FileLine</code> 很少用到.</p>
<h2 id="定制的-CallerName-函数"><a href="#定制的-CallerName-函数" class="headerlink" title="定制的 CallerName 函数"></a>定制的 CallerName 函数</h2><p>基于前面的几个函数, 我们可以方便的定制一个 <code>CallerName</code> 函数. 函数 <code>CallerName</code> 返回调用者的函数名/文件名/行号等用户友好的信息.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">func CallerName(skip int) (name, file string, line int, ok bool) &#123;</span><br><span class="line">	var pc uintptr</span><br><span class="line">	if pc, file, line, ok = runtime.Caller(skip + 1); !ok &#123;</span><br><span class="line">		return</span><br><span class="line">	&#125;</span><br><span class="line">	name = runtime.FuncForPC(pc).Name()</span><br><span class="line">	return</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>其中在执行 <code>runtime.Caller</code> 调用时, 参数 <code>skip + 1</code> 用于抵消 <code>CallerName</code> 函数自身的调用.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">func main() &#123;</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		name, file, line, ok := CallerName(skip)</span><br><span class="line">		if !ok &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v\n&quot;, skip)</span><br><span class="line">		fmt.Printf(&quot;  file = %v, line = %d\n&quot;, file, line)</span><br><span class="line">		fmt.Printf(&quot;  name = %v\n&quot;, name)</span><br><span class="line">	&#125;</span><br><span class="line">	// Output:</span><br><span class="line">	// skip = 0</span><br><span class="line">	//   file = caller.go, line = 19</span><br><span class="line">	//   name = main.main</span><br><span class="line">	// skip = 1</span><br><span class="line">	//   file = C:/go/go-tip/src/pkg/runtime/proc.c, line = 220</span><br><span class="line">	//   name = runtime.main</span><br><span class="line">	// skip = 2</span><br><span class="line">	//   file = C:/go/go-tip/src/pkg/runtime/proc.c, line = 1394</span><br><span class="line">	//   name = runtime.goexit</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Go-语言中函数的类型"><a href="#Go-语言中函数的类型" class="headerlink" title="Go 语言中函数的类型"></a>Go 语言中函数的类型</h2><p>在 Go 语言中, 除了语言定义的普通函数调用外, 还有闭包函数/init函数/全局变量初始化等不同的函数调用类型.</p>
<p>为了便于测试不同类型的函数调用, 我们包装一个 <code>PrintCallerName</code> 函数. 该函数用于输出调用者的信息.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">func PrintCallerName(skip int, comment string) bool &#123;</span><br><span class="line">	name, file, line, ok := CallerName(skip + 1)</span><br><span class="line">	if !ok &#123;</span><br><span class="line">		return false</span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Printf(&quot;skip = %v, comment = %s\n&quot;, skip, comment)</span><br><span class="line">	fmt.Printf(&quot;  file = %v, line = %d\n&quot;, file, line)</span><br><span class="line">	fmt.Printf(&quot;  name = %v\n&quot;, name)</span><br><span class="line">	return true</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后编写以下的测试代码(函数闭包调用/全局变量初始化/init函数等):</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">var a = PrintCallerName(0, &quot;main.a&quot;)</span><br><span class="line">var b = PrintCallerName(0, &quot;main.b&quot;)</span><br><span class="line"></span><br><span class="line">func init() &#123;</span><br><span class="line">	a = PrintCallerName(0, &quot;main.init.a&quot;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func init() &#123;</span><br><span class="line">	b = PrintCallerName(0, &quot;main.init.b&quot;)</span><br><span class="line">	func() &#123;</span><br><span class="line">		b = PrintCallerName(0, &quot;main.init.b[1]&quot;)</span><br><span class="line">	&#125;()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">	a = PrintCallerName(0, &quot;main.main.a&quot;)</span><br><span class="line">	b = PrintCallerName(0, &quot;main.main.b&quot;)</span><br><span class="line">	func() &#123;</span><br><span class="line">		b = PrintCallerName(0, &quot;main.main.b[1]&quot;)</span><br><span class="line">		func() &#123;</span><br><span class="line">			b = PrintCallerName(0, &quot;main.main.b[1][1]&quot;)</span><br><span class="line">		&#125;()</span><br><span class="line">		b = PrintCallerName(0, &quot;main.main.b[2]&quot;)</span><br><span class="line">	&#125;()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">// Output:</span><br><span class="line">// skip = 0, comment = main.a</span><br><span class="line">//   file = caller.go, line = 8</span><br><span class="line">//   name = main.init</span><br><span class="line">// skip = 0, comment = main.b</span><br><span class="line">//   file = caller.go, line = 9</span><br><span class="line">//   name = main.init</span><br><span class="line">// skip = 0, comment = main.init.a</span><br><span class="line">//   file = caller.go, line = 12</span><br><span class="line">//   name = main.init·1</span><br><span class="line">// skip = 0, comment = main.init.b</span><br><span class="line">//   file = caller.go, line = 16</span><br><span class="line">//   name = main.init·2</span><br><span class="line">// skip = 0, comment = main.init.b[1]</span><br><span class="line">//   file = caller.go, line = 18</span><br><span class="line">//   name = main.func·001</span><br><span class="line">// skip = 0, comment = main.main.a</span><br><span class="line">//   file = caller.go, line = 23</span><br><span class="line">//   name = main.main</span><br><span class="line">// skip = 0, comment = main.main.b</span><br><span class="line">//   file = caller.go, line = 24</span><br><span class="line">//   name = main.main</span><br><span class="line">// skip = 0, comment = main.main.b[1]</span><br><span class="line">//   file = caller.go, line = 26</span><br><span class="line">//   name = main.func·003</span><br><span class="line">// skip = 0, comment = main.main.b[1][1]</span><br><span class="line">//   file = caller.go, line = 28</span><br><span class="line">//   name = main.func·002</span><br><span class="line">// skip = 0, comment = main.main.b[2]</span><br><span class="line">//   file = caller.go, line = 30</span><br><span class="line">//   name = main.func·003</span><br></pre></td></tr></table></figure>

<p>观察输出结果, 可以发现以下几个规律:</p>
<ul>
<li>全局变量的初始化调用者为 <code>main.init</code> 函数</li>
<li>自定义的 <code>init</code> 函数有一个数字后缀, 根据出现的顺序进编号. 比如 <code>main.init·1</code> 和 <code>main.init·2</code> 等.</li>
<li>闭包函数采用 <code>main.func·001</code> 格式命名, 安装闭包定义结束的位置顺序进编号.</li>
</ul>
<h2 id="不同-Go-程序启动流程"><a href="#不同-Go-程序启动流程" class="headerlink" title="不同 Go 程序启动流程"></a>不同 Go 程序启动流程</h2><p>基于函数调用者信息可以很容易的验证各种环境的程序启动流程.</p>
<p>我们需要建立一个独立的 <code>caller</code> 目录, 里面有三个测试代码.</p>
<p><code>caller/main.go</code> 主程序:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line">import (</span><br><span class="line">	&quot;fmt&quot;</span><br><span class="line">	&quot;regexp&quot;</span><br><span class="line">	&quot;runtime&quot;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">	_ = PrintCallerName(0, &quot;main.main._&quot;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func PrintCallerName(skip int, comment string) bool &#123;</span><br><span class="line">	// 实现和前面的例子相同</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func CallerName(skip int) (name, file string, line int, ok bool) &#123;</span><br><span class="line">	// 实现和前面的例子相同</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>caller/main_test.go</code> 主程序的测试文件(同在一个<code>main</code>包):</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line">import (</span><br><span class="line">	&quot;fmt&quot;</span><br><span class="line">	&quot;testing&quot;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func TestPrintCallerName(t *testing.T) &#123;</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		name, file, line, ok := CallerName(skip)</span><br><span class="line">		if !ok &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v, name = %v, file = %v, line = %v\n&quot;, skip, name, file, line)</span><br><span class="line">	&#125;</span><br><span class="line">	t.Fail()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>caller/example_test.go</code> 主程序的包的调用者(在新的<code>main_test</code>包):</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">package main_test</span><br><span class="line"></span><br><span class="line">import (</span><br><span class="line">	myMain &quot;.&quot;</span><br><span class="line">	&quot;fmt&quot;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func Example() &#123;</span><br><span class="line">	for skip := 0; ; skip++ &#123;</span><br><span class="line">		name, file, line, ok := myMain.CallerName(skip)</span><br><span class="line">		if !ok &#123;</span><br><span class="line">			break</span><br><span class="line">		&#125;</span><br><span class="line">		fmt.Printf(&quot;skip = %v, name = %v, file = %v, line = %v\n&quot;, skip, name, file, line)</span><br><span class="line">	&#125;</span><br><span class="line">	// Output: ?</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后进入 <code>caller</code> 目录, 运行 <code>go run test</code> 可以得到以下的输出结果:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">skip = 0, name = caller.TestPrintCallerName, file = caller/main_test.go, line = 10</span><br><span class="line">skip = 1, name = testing.tRunner, file = $(GOROOT)/src/pkg/testing/testing.go, line = 391</span><br><span class="line">skip = 2, name = runtime.goexit, file = $(GOROOT)/src/pkg/runtime/proc.c, line = 1394</span><br><span class="line">--- FAIL: TestPrintCallerName (0.00 seconds)</span><br><span class="line">--- FAIL: Example (2.0001ms)</span><br><span class="line">got:</span><br><span class="line">skip = 0, name = caller_test.Example, file = caller/example_test.go, line = 10</span><br><span class="line"></span><br><span class="line">skip = 1, name = testing.runExample, file = $(GOROOT)/src/pkg/testing/example.go, line = 98</span><br><span class="line">skip = 2, name = testing.RunExamples, file = $(GOROOT)/src/pkg/testing/example.go, line = 36</span><br><span class="line">skip = 3, name = testing.Main, file = $(GOROOT)/src/pkg/testing/testing.go, line = 404</span><br><span class="line">skip = 4, name = main.main, file = $(TEMP)/go-build365033523/caller/_test/_testmain.go, line = 51</span><br><span class="line">skip = 5, name = runtime.main, file = $(GOROOT)/src/pkg/runtime/proc.c, line = 220</span><br><span class="line">skip = 6, name = runtime.goexit, file = $(GOROOT)/src/pkg/runtime/proc.c, line = 1394</span><br><span class="line">want:</span><br><span class="line">?</span><br><span class="line">FAIL</span><br><span class="line">exit status 1</span><br><span class="line">FAIL    caller        0.254s</span><br></pre></td></tr></table></figure>

<p>分析输出数据我们可以发现, 测试代码和例子代码的启动流程和普通的程序流程都不太一样.</p>
<p><code>测试代码</code>的启动流程:</p>
<ol>
<li><code>runtime.goexit</code> 还是入口</li>
<li>但是 <code>runtime.goexit</code> 不在调用 <code>runtime.main</code> 函数, 而是调用 <code>testing.tRunner</code> 函数</li>
<li><code>testing.tRunner</code> 函数由 <code>go test</code> 命令生成, 用于执行各个测试函数</li>
</ol>
<p><code>例子代码</code>的启动流程:</p>
<ol>
<li><code>runtime.goexit</code> 还是入口</li>
<li>然后 <code>runtime.goexit</code> 调用 <code>runtime.main</code> 函数</li>
<li>最终 <code>runtime.main</code> <strong>调用go test 命令生成的 main.main 函数</strong>, 在 <code>_test/_testmain.go</code> 文件</li>
<li>然后调用 <code>testing.Main</code>, 改函数执行各个例子函数</li>
</ol>
<p>另外, 从这个例子我们可以发现, 我们自己写的 <code>main.main</code> 函数所在的 <code>main</code> 包也 可以被其他包导入. 但是其他包导入之后的 <code>main</code> 包里的 <code>main</code> 函数就不再是 <code>main.main</code> 函数了. 因此, 程序的入口也就不是自己写的 <code>main.main</code> 函数了.</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>Go 语言 <code>runtime</code> 包的 <code>runtime.Caller</code> / <code>runtime.Callers</code> / <code>runtime.FuncForPC</code> 等函数虽然看起来比较简单, 但是功能却非常强大.</p>
<p>这几个函数不仅可以解决一些实际的工程问题 , 而且非常适合用于调试和分析各种Go程序的运行时信息.</p>

    </div>

    
    
    
        
      
        <div id="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作, 一个耿直的笑脸~</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Levon 微信支付">
        <p>微信支付</p>
      </div>
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.jpg" alt="Levon 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/golang/" rel="tag"># golang</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/p/4c5612cb.html" rel="next" title="golang闭包的坑">
                  <i class="fa fa-chevron-left"></i> golang闭包的坑
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/p/bf531b37.html" rel="prev" title="transmission编译安装和golang_rpc的调用">
                  transmission编译安装和golang_rpc的调用 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    
  <div class="comments" id="comments">
    <div id="disqus_thread">
      <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
    </div>
  </div>
  
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#runtime-Caller的用法-常用"><span class="nav-number">1.</span> <span class="nav-text">runtime.Caller的用法(常用)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#runtime-Callers的用法"><span class="nav-number">2.</span> <span class="nav-text">runtime.Callers的用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#runtime-Callers-和-runtime-Caller-的异同"><span class="nav-number">3.</span> <span class="nav-text">runtime.Callers 和 runtime.Caller 的异同</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#runtime-FuncForPC-的用途"><span class="nav-number">4.</span> <span class="nav-text">runtime.FuncForPC 的用途</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#定制的-CallerName-函数"><span class="nav-number">5.</span> <span class="nav-text">定制的 CallerName 函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Go-语言中函数的类型"><span class="nav-number">6.</span> <span class="nav-text">Go 语言中函数的类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#不同-Go-程序启动流程"><span class="nav-number">7.</span> <span class="nav-text">不同 Go 程序启动流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">8.</span> <span class="nav-text">总结</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Levon">
  <p class="site-author-name" itemprop="name">Levon</p>
  <div class="site-description" itemprop="description">Never Give Up</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">34</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">76</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/unix2dos" title="GitHub &rarr; https://github.com/unix2dos" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:levonfly@gmail.com" title="E-Mail &rarr; mailto:levonfly@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://twitter.com/levonfly" title="Twitter &rarr; https://twitter.com/levonfly" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://weibo.com/l6241425" title="Weibo &rarr; https://weibo.com/l6241425" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Levon</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">507k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:41</span>
</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  <script src="/js/local-search.js?v=7.4.0"></script>














  

  

  

<script>
  function loadCount() {
    var d = document, s = d.createElement('script');
    s.src = 'https://levonfly.disqus.com/count.js';
    s.id = 'dsq-count-scr';
    (d.head || d.body).appendChild(s);
  }
  // defer loading until the whole page loading is completed
  window.addEventListener('load', loadCount, false);
</script>
<script>
  function disqus_config() {
    this.page.url = "https://unix2dos.github.io/p/375281af.html";
    this.page.identifier = "p/375281af.html";
    this.page.title = 'golang_runtime函数调用信息';};
  function loadComments() {
    if (window.DISQUS) {
      DISQUS.reset({
        reload: true,
        config: disqus_config
      });
    } else {
      var d = document, s = d.createElement('script');
      s.src = 'https://levonfly.disqus.com/embed.js';
      s.setAttribute('data-timestamp', '' + +new Date());
      (d.head || d.body).appendChild(s);
    }
  }
    window.addEventListener('load', loadComments, false);
  
</script>

</body>
</html>
